/**
 * @author luther.seven@gmail.com
 */

var forecastXHR;

// XML Namespace resolver for XPath namespace resolution
function NSResolver(prefix) {
  var ns = {
    'georss' : 'http://www.georss.org/georss',
    'aws': 'http://www.aws.com/aws',
  };
  return ns[prefix] || null;
}

// Takes a definition document and populates the various DCTK controls
function handleForecastResult(doc)
{
    // extract information with XPath
	
	var main = doc.evaluate("//channel/title", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	var mainDesc = doc.evaluate("//channel/description", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	var poster = doc.evaluate("//channel/image/url", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
	
	var results = doc.evaluate("count(//channel/item)", doc, NSResolver, XPathResult.ANY_TYPE, null).numberValue;
	
	var title = new Array()
	for (var i = 1; i <= results; i++){
		title[i] = doc.evaluate("//channel/item[" + i + "]/title", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
	var date = new Array()
	for (var i = 1; i <= results; i++){
		date[i] = doc.evaluate("//channel/item["+ i + "]/pubDate", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
	var desc = new Array()
	for (var i = 1; i <= results; i++){
		desc[i] = doc.evaluate("//channel/item["+ i + "]/description", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
    
    var dctk = connected.toolkit;
/*
	mainTitle = dctk.getElementById("mainTitle");
	mainTitle.text = "Daily Motion - "+mainDesc;
	
	queueTxt = dctk.getElementById("queueTxt");
	queueTxt.text = "Press UP/DOWN to change channels...";
	
	logo = dctk.getElementById("logo");
	logo.img.src = poster;
*/	

	var getOne = desc[2].split('<b>')[1].split('<\/b>')[0];
	var titleOne = title[2].split("'");
	valueOne = dctk.getElementById("valueOne");
	valueOne.text = titleOne[0]+": "+getOne;
	
	var getIconOne = desc[2].split('img src=\"')[1].split('\" border')[0];
	iconOne = dctk.getElementById("iconOne");
	iconOne.img.src = getIconOne;
	
	var getTwo = desc[3].split('<b>')[1].split('<\/b>')[0];
	var titleTwo = title[3].split("'");
	valueTwo = dctk.getElementById("valueTwo");
	valueTwo.text = titleTwo[0]+": "+getTwo;
	
	var getIconTwo = desc[3].split('img src=\"')[1].split('\" border')[0];
	iconTwo = dctk.getElementById("iconTwo");
	iconTwo.img.src = getIconTwo;
	
	var getThree = desc[4].split('<b>')[1].split('<\/b>')[0];
	var titleThree = title[4].split("'");
	valueThree = dctk.getElementById("valueThree");
	valueThree.text = titleThree[0]+": "+getThree;
	
	var getIconThree = desc[4].split('img src=\"')[1].split('\" border')[0];
	iconThree = dctk.getElementById("iconThree");
	iconThree.img.src = getIconThree;
	
	var getFour = desc[5].split('<b>')[1].split('<\/b>')[0];
	var titleFour = title[5].split("'");
	valueFour = dctk.getElementById("valueFour");
	valueFour.text = titleFour[0]+": "+getFour;
	
	var getIconFour = desc[5].split('img src=\"')[1].split('\" border')[0];
	iconFour = dctk.getElementById("iconFour");
	iconFour.img.src = getIconFour;
}

function onForecastXHRStateChange()
{
    if (forecastXHR.readyState == 4) {
        if (forecastXHR.status == 200) {
            handleForecastResult(forecastXHR.responseXML);
        }
    }
}

function startForecastRequest()
{
	forecastXHR = new XMLHttpRequest();
	forecastXHR.open("GET", "http://api.wxbug.net/getForecastRSS.aspx?ACode="+apiKey+"&"+zipCode, true);
	forecastXHR.overrideMimeType('text/xml');
	forecastXHR.onreadystatechange = onForecastXHRStateChange;
	forecastXHR.send(null); 
}